﻿// Decompiled with JetBrains decompiler
// Type: Microsoft.InfoCards.GetBrowserTokenEndpoint
// Assembly: infocard, Version=3.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089
// MVID: 8E14765A-6610-409A-BA36-099A0642905D
// Assembly location: E:\git\ALLIDA\windll\infocard.exe

using System;
using System.IO;
using System.Security.Cryptography.X509Certificates;
using System.ServiceModel;
using System.ServiceModel.Channels;
using System.Text;
using System.Xml;

namespace Microsoft.InfoCards
{
  internal class GetBrowserTokenEndpoint
  {
    private X509Certificate2Collection m_supportingCertsCollection = new X509Certificate2Collection();
    private Uri m_address;
    private Uri m_policyAddress;
    private X509Certificate2 m_leafCert;

    public Uri Address
    {
      get
      {
        return this.m_address;
      }
    }

    public Uri PolicyUrl
    {
      get
      {
        return this.m_policyAddress;
      }
    }

    public X509Certificate2Collection SupportingCertificates
    {
      get
      {
        return this.m_supportingCertsCollection;
      }
    }

    public X509Certificate2 LeafCertificate
    {
      get
      {
        return this.m_leafCert;
      }
    }

    public EndpointAddress CreateEndpointAddress()
    {
      if (!((Uri) null != this.m_address))
        return (EndpointAddress) null;
      if (this.LeafCertificate != null)
        return new EndpointAddress(this.m_address, EndpointIdentity.CreateX509CertificateIdentity(this.LeafCertificate, this.SupportingCertificates), new AddressHeader[0]);
      return new EndpointAddress(this.m_address, new AddressHeader[0]);
    }

    public XmlElement CreateEndpointAddressXml()
    {
      if ((Uri) null != this.m_address)
      {
        EndpointAddress endpointAddress = this.LeafCertificate == null ? new EndpointAddress(this.m_address, new AddressHeader[0]) : new EndpointAddress(this.m_address, EndpointIdentity.CreateX509CertificateIdentity(this.LeafCertificate, this.SupportingCertificates), new AddressHeader[0]);
        if ((EndpointAddress) null != endpointAddress)
        {
          using (MemoryStream memoryStream = new MemoryStream())
          {
            using (XmlWriter writer = (XmlWriter) new XmlTextWriter((Stream) memoryStream, Encoding.UTF8))
            {
              endpointAddress.WriteTo(AddressingVersion.WSAddressing10, writer);
              writer.Flush();
              memoryStream.Flush();
              memoryStream.Seek(0L, SeekOrigin.Begin);
              using (XmlReader readerWithQuotas = (XmlReader) Utility.CreateReaderWithQuotas((Stream) memoryStream))
                return (XmlElement) new XmlDocument().ReadNode(readerWithQuotas);
            }
          }
        }
      }
      return (XmlElement) null;
    }

    public void Load(BinaryReader reader, int paramVersion)
    {
      string uriString1 = Utility.DeserializeString(reader);
      if (!string.IsNullOrEmpty(uriString1))
        this.m_address = new Uri(uriString1);
      string uriString2 = Utility.DeserializeString(reader);
      if (!string.IsNullOrEmpty(uriString2))
        this.m_policyAddress = new Uri(uriString2);
      int num = 0;
      if (2 == paramVersion)
        num = reader.ReadInt32();
      else if (1 == paramVersion)
        num = 1;
      for (int index = 0; index < num; ++index)
      {
        byte[] rawData = reader.ReadBytes(reader.ReadInt32());
        if (!Utility.ArrayIsNullOrEmpty((Array) rawData))
        {
          X509Certificate2 certificate = new X509Certificate2(rawData);
          if (index == 0)
            this.m_leafCert = certificate;
          else
            this.m_supportingCertsCollection.Add(certificate);
        }
      }
    }
  }
}
